home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / upc12bs1.zip / LIB / safeio.c < prev    next >
C/C++ Source or Header  |  1993-10-02  |  9KB  |  256 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    s a f e i o . c                                                 */
  3. /*                                                                    */
  4. /*    Console I/O functions for use during interrupt processing       */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1989-1993 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: safeio.c 1.5 1993/10/03 00:03:45 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: safeio.c $
  24.  *     Revision 1.5  1993/10/03  00:03:45  ahd
  25.  *     Only use currentfile() under Windows NT
  26.  *
  27.  *     Revision 1.4  1993/09/20  04:39:51  ahd
  28.  *     OS/2 2.x support
  29.  *
  30.  *     Revision 1.3  1993/07/20  21:42:43  dmwatt
  31.  *     Don't rely on standard I/O under Windows/NT
  32.  *
  33.  */
  34.  
  35. /*--------------------------------------------------------------------*/
  36. /*    Since C I/O functions are not safe inside signal routines,      */
  37. /*    the code uses conditionals to use system-level DOS and OS/2     */
  38. /*    services.  Another option is to set global flags and do any     */
  39. /*    I/O operations outside the signal handler.                      */
  40. /*--------------------------------------------------------------------*/
  41.  
  42. #define __MSC                 /* Make Borland C++ 2.0 act like MS C  */
  43.  
  44. #include <stdio.h>
  45.  
  46. #if defined( WIN32 )
  47.     #include <windows.h>
  48.     #include <string.h>
  49. #elif defined( FAMILYAPI ) || defined(__OS2__)
  50.     #define INCL_NOCOMMON
  51.     #define INCL_NOPM
  52.     #define INCL_VIO
  53.     #define INCL_KBD
  54.     #include <os2.h>
  55.     #include <string.h>
  56. #else /* FAMILYAPI */
  57.     #include <conio.h>
  58.     #include <dos.h>
  59.     #include <bios.h>
  60. #endif
  61.  
  62. /*--------------------------------------------------------------------*/
  63. /*                    UUPC/extended include files                     */
  64. /*--------------------------------------------------------------------*/
  65.  
  66. #include "lib.h"
  67. #include "safeio.h"
  68.  
  69. /*--------------------------------------------------------------------*/
  70. /*                          Global variables                          */
  71. /*--------------------------------------------------------------------*/
  72.  
  73. #if defined( WIN32 )
  74. currentfile();
  75. #endif
  76.  
  77. /*--------------------------------------------------------------------*/
  78. /*    s a f e i n                                                     */
  79. /*                                                                    */
  80. /*    Inputs a character using system level calls.  From MicroSoft    */
  81. /*    Programmer's Workbench QuickHelp samples                        */
  82. /*--------------------------------------------------------------------*/
  83.  
  84. #if defined(WIN32)
  85.  
  86. static HANDLE hConsoleIn = INVALID_HANDLE_VALUE;
  87.  
  88. /*--------------------------------------------------------------------*/
  89. /*       I n i t C o n s o l e I n p u t H a n d l e                  */
  90. /*                                                                    */
  91. /*       Initialize Window NT console handle allow reading            */
  92. /*       from console when stdin is redirected.                       */
  93. /*--------------------------------------------------------------------*/
  94.  
  95. void InitConsoleInputHandle(void)
  96. {
  97.    hConsoleIn = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, 0, NULL,
  98.       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  99.  
  100.    if (hConsoleIn == INVALID_HANDLE_VALUE) {
  101.       printmsg(0, "InitConsoleInputHandle:  could not open console handles!");
  102.       panic();
  103.    }
  104. }
  105. #endif
  106.  
  107.  
  108. int safein( void )
  109. {
  110. #ifdef _Windows
  111.  
  112. /*--------------------------------------------------------------------*/
  113. /*                       Windows get character                        */
  114. /*--------------------------------------------------------------------*/
  115.  
  116.    return getchar( );
  117.  
  118. #elif defined( WIN32 )
  119.  
  120. /*--------------------------------------------------------------------*/
  121. /*                      Windows NT get character                      */
  122. /*--------------------------------------------------------------------*/
  123.  
  124.    CHAR ch;
  125.    DWORD dwBytesRead;
  126.  
  127.    if (hConsoleIn == INVALID_HANDLE_VALUE)
  128.       InitConsoleInputHandle();
  129.  
  130.    ReadFile(hConsoleIn, &ch, 1, &dwBytesRead, NULL);
  131.  
  132.    return ch;
  133.  
  134. #elif defined( FAMILYAPI ) || defined( __OS2__ )
  135.  
  136. /*--------------------------------------------------------------------*/
  137. /*                         OS/2 Get character                         */
  138. /*--------------------------------------------------------------------*/
  139.  
  140.     KBDKEYINFO kki;
  141.  
  142.     KbdCharIn( &kki, IO_WAIT, 0 );
  143.     return kki.chChar;
  144.  
  145. #else /* FAMILYAPI */
  146.  
  147. /*--------------------------------------------------------------------*/
  148. /*                         DOS get character                          */
  149. /*--------------------------------------------------------------------*/
  150.  
  151.     int c = (_bios_keybrd( _KEYBRD_READ ) & 0xff );
  152.     union REGS inregs, outregs;
  153.  
  154.     inregs.h.ah = 0x0e;
  155.     inregs.h.al = (char) c;
  156.     int86( 0x10, &inregs, &outregs );
  157.     return c;
  158.  
  159. #endif
  160.  
  161. } /* safein */
  162.  
  163. /*--------------------------------------------------------------------*/
  164. /*    s a f e p e e k                                                 */
  165. /*                                                                    */
  166. /*    Determine if a character is waiting at the keyboard for us.     */
  167. /*    Written by ahd based on safein (above).                         */
  168. /*--------------------------------------------------------------------*/
  169.  
  170. boolean safepeek( void )
  171. {
  172.  
  173. #ifdef _Windows
  174.  
  175.    return 0;
  176.  
  177. #elif defined(WIN32)
  178.  
  179.    INPUT_RECORD Buffer;
  180.    DWORD nEventsRead;
  181.  
  182.    if (hConsoleIn == INVALID_HANDLE_VALUE)
  183.       InitConsoleInputHandle();
  184.  
  185.    PeekConsoleInput(hConsoleIn, &Buffer, 1, &nEventsRead);
  186.  
  187.    if (nEventsRead != 0 && Buffer.EventType == KEY_EVENT)
  188.       return TRUE;
  189.    return FALSE;
  190.  
  191. #elif defined( FAMILYAPI ) || defined(__OS2__)
  192.  
  193.     KBDKEYINFO kki;
  194.  
  195.     KbdPeek( &kki, 0 );
  196.  
  197. #ifdef __OS2__
  198.     return (kki.fbStatus & KBDTRF_FINAL_CHAR_IN);
  199. #else
  200.     return (kki.fbStatus & FINAL_CHAR_IN);
  201. #endif
  202.  
  203. #else /* FAMILYAPI */
  204.  
  205. /*--------------------------------------------------------------------*/
  206. /*                         DOS Keyboard peek                          */
  207. /*--------------------------------------------------------------------*/
  208.  
  209.     return (_bios_keybrd( _KEYBRD_READY ) & 0xff );
  210.  
  211. #endif /* _Windows */
  212.  
  213. } /* safepeek */
  214.  
  215. /*--------------------------------------------------------------------*/
  216. /*    s a f e f l u s h                                               */
  217. /*                                                                    */
  218. /*    Flush the keyboard look ahead buffer.                           */
  219. /*    Written by ahd based on safein (above).                         */
  220. /*--------------------------------------------------------------------*/
  221.  
  222. void safeflush( void )
  223. {
  224.  
  225. #ifdef _Windows
  226.  
  227.    return;
  228.  
  229. #elif defined(WIN32)
  230.  
  231.    if (hConsoleIn == INVALID_HANDLE_VALUE)
  232.       InitConsoleInputHandle();
  233.  
  234.    FlushConsoleInputBuffer(hConsoleIn);
  235.  
  236. #elif defined( FAMILYAPI ) || defined(__OS2__)
  237.  
  238.     KbdFlushBuffer( 0 );      /* That's all!  (Makes you love rich
  239.                                  API's, doesn't it?)                 */
  240.  
  241. #else
  242.  
  243. /*--------------------------------------------------------------------*/
  244. /*                         DOS Keyboard flush                         */
  245. /*--------------------------------------------------------------------*/
  246.  
  247.    union REGS regs;
  248.  
  249.    regs.h.ah = 0x0C;       /* Flush buffer, read keyboard            */
  250.    regs.h.al = 0x00;       /* Don't actually read keyboard           */
  251.    intdos( ®s, ®s ); /* Make it happen                         */
  252.  
  253. #endif /* _Windows */
  254.  
  255. } /* safeflush */
  256.